package com.mcintyret.site.journal;

import com.mcintyret.site.users.OwnedByUser;

import javax.persistence.*;
import java.util.Date;

/**
 * User: mcintyret2
 * Date: 28/03/2012
 */

@Entity
@Table(name = "text_entries")
public class TextEntry implements OwnedByUser {

    private Integer id;
    private Journal journal;
    private String username;
    private String text;
    private Date time;

    // For use by Hibernate
    TextEntry() {}

    public TextEntry(String text) {
        setText(text);
        setTime(new Date());
    }

    @Id
    @GeneratedValue
    @Column(name = "text_entry_id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @ManyToOne(optional = false)
    @JoinColumn(name = "journal_id")
    public Journal getJournal() {
        return journal;
    }

    public void setJournal(Journal journal) {
        this.journal = journal;
    }

    @Column(name = "username")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "time")
    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    @Column(name = "text")
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        TextEntry textEntry = (TextEntry) o;

        if (journal != null ? !journal.equals(textEntry.journal) : textEntry.journal != null) {
            return false;
        }
        if (text != null ? !text.equals(textEntry.text) : textEntry.text != null) {
            return false;
        }
        if (time != null ? !time.equals(textEntry.time) : textEntry.time != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = journal != null ? journal.hashCode() : 0;
        result = 31 * result + (text != null ? text.hashCode() : 0);
        result = 31 * result + (time != null ? time.hashCode() : 0);
        return result;
    }
}
